package cn.ncu.wechat.service.impl;

import cn.ncu.wechat.model.Admin;
import cn.ncu.wechat.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.ncu.wechat.model.Store;
import cn.ncu.wechat.service.StoreService;
import cn.ncu.wechat.dao.StoreMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

/**
* @author 
* @description 针对表【store(门店表)】的数据库操作Service实现
* @createDate 2025-01-07 16:20:04
*/
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store>
    implements StoreService{

    @Override
    public Result<?> getStoreList(Integer page, Integer pageSize, String keyword) {
        if ("-1".equals(keyword)) {
            keyword = null;
        }
        try{
            Page<Store> pageParam = new Page<>(page, pageSize);
            LambdaQueryWrapper<Store> queryWrapper = new LambdaQueryWrapper<>();
            if (keyword != null) {
                String finalKeyword = keyword;
                queryWrapper.and(wrapper ->
                        wrapper.eq(Store::getName, finalKeyword)
                );
            }
            Page<Store> pageResult = page(pageParam, queryWrapper);
            return Result.success(pageResult);
        } catch (Exception e) {
            return Result.error(500,"获取门店列表失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<?> addOrUpdateStore(Map<String, Object> request) {
        try {
            String name = String.valueOf(request.get("name"));
            String address = String.valueOf(request.get("address"));
            String phone = String.valueOf(request.get("phone"));
            String manager = String.valueOf(request.get("manager"));
            Integer status = Integer.valueOf(String.valueOf(request.get("status")));
            
            if ("null".equals(name) || name.trim().isEmpty()) {
                return Result.error(400, "门店名称不能为空");
            }
            if ("null".equals(address) || address.trim().isEmpty()) {
                return Result.error(400, "门店地址不能为空");
            }
            if ("null".equals(phone) || phone.trim().isEmpty()) {
                return Result.error(400, "联系电话不能为空");
            }
            if ("null".equals(manager) || manager.trim().isEmpty()) {
                return Result.error(400, "店长不能为空");
            }
            if (status == null || (status != 1 && status != 2)) {
                return Result.error(400, "状态值只能是1或2");
            }

            LambdaQueryWrapper<Store> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Store::getName, name);
            
            Store store;
            if (request.get("id") != null) {
                Long id = Long.valueOf(String.valueOf(request.get("id")));
                store = getById(id);
                if (store == null) {
                    return Result.error(404, "门店不存在");
                }
                
                queryWrapper.ne(Store::getId, id);
                if (count(queryWrapper) > 0) {
                    return Result.error(400, "门店名称已存在");
                }
                
                store.setName(name);
                store.setAddress(address);
                store.setPhone(phone);
                store.setManager(manager);
                store.setStatus(status);

                boolean success = updateById(store);
                if (success) {
                    return Result.success("更新成功");
                } else {
                    return Result.error(500, "更新失败");
                }
                
            } else {
                if (count(queryWrapper) > 0) {
                    return Result.error(400, "门店名称已存在");
                }
                
                store = new Store();
                store.setName(name);
                store.setAddress(address);
                store.setPhone(phone);
                store.setManager(manager);
                store.setStatus(status);
                store.setCreateTime(new Date());

                boolean success = save(store);
                if (success) {
                    return Result.success("新增成功");
                } else {
                    return Result.error(500, "新增失败");
                }
            }
        } catch (Exception e) {
            return Result.error(500,"操作失败" + e.getMessage());
        }
    }
}




